﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Web.Security;
using EnetaMvc.ApplicationServices;
using log4net;
using StructureMap;

namespace EnetaMvc.Web.Security
{
    public class EnetaRoleProvider : RoleProvider
    {
        private IUserService _userService;
        private ILog _log;

        public EnetaRoleProvider()
        {
            log4net.Config.XmlConfigurator.Configure();
            _log = LogManager.GetLogger(typeof(MvcApplication));

            //ObjectFactory.Initialize(container =>
            //{
            //    container.PullConfigurationFromAppConfig = true;
            //});

            try
            {
                _userService = ObjectFactory.GetInstance<IUserService>();
            }
            catch(Exception ex)
            {
                _log.Error("EnetaMvc Web App: Exception was thrown");
                _log.Error(ex.ToString());
            }
        }

        public override string[] GetAllRoles()
        {
            var roleNames = _userService.ListRoles();

            _log.Debug("GetAllRoles: roleNames = " + string.Join(", ", roleNames));

            return roleNames;
        }

        public override string[] GetRolesForUser(string username)
        {
            var user = _userService.GetUserByToken(username);
            var roleNames = _userService.GetUserRoles(user.Id);

            _log.Debug("GetRolesForUser: " + username);
            _log.Debug("GetRolesForUser: role names = " + string.Join(", ", roleNames));

            return roleNames;
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var user = _userService.GetUserByToken(username);
            var roles = _userService.GetUserRoles(user.Id);

            return roles.Contains(roleName);
        }

        public override bool RoleExists(string roleName)
        {
            var roles = _userService.ListRoles();
            return roles.Contains(roleName);
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new System.NotImplementedException();
        }

        public override string ApplicationName { get; set; }

        public override void CreateRole(string roleName)
        {
            throw new System.NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new System.NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new System.NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new System.NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new System.NotImplementedException();
        }
    }
}